import java.util.ArrayList;
import java.util.List;

public class BSTIterator {
    private List<Integer> list = new ArrayList<>();
    int count = 0;
    public BSTIterator(TreeNode root) {
        dfs(root);
    }
    public void dfs(TreeNode root){
        if (root==null){
            return;
        }
        dfs(root.left);
        list.add(root.val);
        dfs(root.right);
    }

    public int next() {
        return list.get(count++);
    }

    public boolean hasNext() {
        try {
            list.get(count+1);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
